home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / truffles - display mgr. / sprocket / experimentalstuff / mailablewindow.cp < prev    next >
Encoding:
Text File  |  1996-01-01  |  6.2 KB  |  273 lines

  1. /*
  2.     File:        MailableWindow.cp
  3.  
  4.     Contains:    A AOCE-aware window base class
  5.                 
  6.     Written by: Dave Falkenburg with help from Steve Falkenburg’s
  7.                 CollaboDraw “object oriented C” sample application.
  8.     
  9.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.     
  13.          <4>     9/27/94    DRF        Changes for Dave Mark: AppLib.h is now Sprocket.h.
  14.          <3>      9/9/94    DRF        Reordered headers and removed redundant #includes and
  15.                                     conditionalize AOCE support.
  16.          <2>     8/26/94    DRF        Added AdjustPerfectWindowSizeForMailer.    
  17.  */
  18.  
  19. #include "Sprocket.h"
  20. #include "MailableWindow.h"
  21.  
  22. #if    qAOCEAware
  23.  
  24. TMailableWindow::TMailableWindow()
  25.     {
  26.     //    Remember, we can’t do anything in here because of C++’s insistance
  27.     //    of creating objects from the bottom-up.  If we call CreateWindow in
  28.     //    here then we end up calling the pure-virtual version of MakeNewWindow.
  29.  
  30.     fMailerIsAttached = false;
  31.     fMailerIsExpanded = false;
  32.     }
  33.  
  34.  
  35. TMailableWindow::~TMailableWindow()
  36.     {
  37. #if    qDebug
  38.     //    Release any PowerTalk things if they are still around
  39.  
  40.     if (fMailerIsAttached)
  41.         DebugStr("\pDidn’t call TMailableWindow::Close to dispose mailer");
  42. #endif
  43.     }
  44.  
  45.  
  46. Boolean
  47. TMailableWindow::EventFilter(EventRecord * anEvent)
  48.     {
  49.     if (gHasAOCE)
  50.         {
  51.         SMPMailerResult    whatHappened;
  52.         SMPMailerState    mailerState;
  53.         
  54.         (void) SMPMailerEvent(anEvent,&whatHappened,FrontWindowProcForAOCEUPP,0);
  55.         (void) SMPGetMailerState(fWindow,&mailerState);
  56.  
  57.         //    ALOT MORE STUFF GOES IN HERE!
  58.  
  59.         // track if the mailer has been expanded or contracted        
  60.         if ((whatHappened & kSMPContractedMask) != 0)
  61.             this->ExpandOrContractMailer(false);
  62.         else if ((whatHappened & kSMPExpandedMask) != 0)
  63.             this->ExpandOrContractMailer(true);
  64.  
  65.         // check to see if the app must handle this event
  66.         if ((whatHappened & kSMPAppMustHandleEventMask) != 0)
  67.             return false;
  68.         else
  69.             return true;    //    nope, PowerTalk dealt with the event, we don’t have to.
  70.         }
  71.  
  72.     return false;
  73.     }
  74.  
  75.  
  76. void
  77. TMailableWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  78.     {
  79. #if    qDebug
  80.     //    You can’t put a mailer in a floating window or a modal window!
  81.  
  82.     if (typeOfWindowToCreate != kNormalWindow)
  83.         DebugStr("\pTMailableWindow: Must be normal windows");
  84. #endif
  85.  
  86.     TWindow::CreateWindow();
  87.     
  88.     if (gHasAOCE && gPreferences.fMailPreferences.fCreateMailerForNewDocuments)
  89.         this->AttachMailerToWindow(true);
  90.  
  91.     fContentRect = fWindow->portRect;
  92.     }
  93.  
  94.  
  95. Boolean
  96. TMailableWindow::Close(void)
  97.     {
  98.     Boolean    reallyClosing = true;
  99.     
  100.     if (fMailerIsAttached)
  101.         {
  102.         //    Offer to send or save the letter if it has changed
  103.  
  104.         }
  105.     else
  106.         {
  107.         //    Offer to save the document if it is dirty
  108.  
  109.         }
  110.  
  111.     if (fMailerIsAttached)
  112.         {
  113.         //    We need to remove the mailer before calling TWindow::Close
  114.         //    otherwise AOCE will get very confused. Too bad they didn’t
  115.         //    integrate better with the window manager to catch this for us.
  116.         
  117.         this->RemoveMailerFromWindow();
  118.         }
  119.  
  120.     if (reallyClosing)
  121.         return TWindow::Close();
  122.  
  123.     return false;
  124.     }
  125.  
  126.  
  127. OSErr
  128. TMailableWindow::AttachMailerToWindow(Boolean createExpanded)
  129.     {
  130.     Point    where = {0,0};
  131.     OSErr    err;
  132.         
  133.     err = SMPNewMailer(fWindow,where,kCanContract,createExpanded,kDefaultAuthIdentity,nil,0L);
  134.     fMailerIsAttached = (err == noErr);
  135.     fMailerIsExpanded = kInitiallyExpanded;
  136.     
  137.     return err;    
  138.     }
  139.  
  140.  
  141. OSErr
  142. TMailableWindow::RemoveMailerFromWindow(void)
  143.     {
  144.     OSErr                err = noErr;
  145.     SMPCloseOptions        closeOptions;
  146.     
  147.     closeOptions.moveToTrash = false;
  148.     closeOptions.addTag = false;
  149.     closeOptions.tag.dataLength = 0;
  150.     
  151.     if (fMailerIsAttached)
  152.         {
  153.         //    Get rid of the PowerTalk mailer header    
  154.         err = SMPDisposeMailer(fWindow,&closeOptions);
  155.         
  156.         fMailerIsAttached = false;
  157.         fContentRect = fWindow->portRect;
  158.         }
  159.         
  160.     return err;
  161.     }
  162.  
  163.  
  164. void
  165. TMailableWindow::Draw(void)
  166.     {
  167.     //    Because of SMPMailerEvent, the PowerTalk mailer, if present, will be drawn by AOCE
  168.     //    This means the window’s draw method only needs to worry drawing the other content
  169.  
  170.     RgnHandle    originalClipRgn = NewRgn();
  171.     
  172.     GetClip(originalClipRgn);
  173.     ClipRect(&fContentRect);
  174.  
  175.     //    Possible optimization by checking content rect against visRgn
  176.     //    « we probably also want to do a wacky SetOrigin call here »    
  177.     this->DrawContents();
  178.  
  179.     SetClip(originalClipRgn);
  180.     DisposeRgn(originalClipRgn);
  181.     }
  182.  
  183.  
  184. void
  185. TMailableWindow::AdjustPerfectWindowSizeForMailer(Rect * perfectSize)
  186.     {
  187.     //    If a mailer is attached to the window, figure out how big it is
  188.     //    and make sure that the window is wide enough to deal with it.
  189.  
  190.     if (fMailerIsAttached)
  191.         {
  192.         short    expHeight,contHeight,mWidth;
  193.     
  194.         (void) SMPGetDimensions(&mWidth,&contHeight,&expHeight);    
  195.     
  196.         //    Make sure the perfect size is wide enough for the standard mailer.
  197.     
  198.         if (perfectSize->right < perfectSize->left + mWidth)
  199.             perfectSize->right = perfectSize->left + mWidth;
  200.  
  201.         //    We might also want to adjust height, but for now ignore the problem
  202.         }
  203.     }
  204.     
  205.  
  206. void
  207. TMailableWindow::AdjustForNewWindowSize(Rect * /* oldRect */,Rect * newRect)
  208.     {
  209.     //    NOTE: Assumes rect is always zero-based
  210.  
  211.     fContentRect.bottom = newRect->bottom;
  212.     fContentRect.right = newRect->right;
  213.     }
  214.  
  215.  
  216. void
  217. TMailableWindow::ExpandOrContractMailer(Boolean doExpand)
  218.     {
  219.     GrafPtr    oldPort;
  220.     Rect    prevContentRect, newContentRect;
  221.     short    expHeight,contHeight,mWidth;
  222.  
  223.     GetPort(&oldPort);
  224.     SetPort(fWindow);
  225.     
  226.     (void) SMPGetDimensions(&mWidth,&contHeight,&expHeight);    
  227.  
  228.     prevContentRect = fContentRect;
  229.     newContentRect = fWindow->portRect;
  230.  
  231.     if (doExpand)
  232.         newContentRect.top += expHeight;
  233.     else
  234.         newContentRect.top += contHeight;
  235.  
  236.     this->AdjustForNewContentRect(&prevContentRect,&newContentRect);
  237.     fContentRect = newContentRect;
  238.  
  239.     (void) SMPExpandOrContract(fWindow,doExpand);
  240.     fMailerIsExpanded = true;
  241.  
  242.     SetPort(oldPort);
  243.     }
  244.  
  245.  
  246. void
  247. TMailableWindow::AdjustForNewContentRect(Rect * /* prevContentRect */,Rect * newContentRect)
  248.     {
  249.     //    the default thing to do is to force a complete redraw of the window’s normal
  250.     //    content area. We could be clever and use scrollrect to keep the bits around,
  251.     //    but for now we cheeze out.
  252.     
  253.     InvalRect(newContentRect);
  254.     }
  255.  
  256. void
  257. TMailableWindow::DrawContents(void)
  258.     {
  259.     }
  260.  
  261.  
  262. //    FrontWindow custom procedure for AOCE standard mail package:
  263. //        It enables some of AOCE to cleanly interoperate with “Dean Yu”-style floating windows
  264.  
  265. pascal    WindowRef
  266. FrontWindowProcForAOCE(long /* unusedParam */)
  267.     {
  268.     return FrontNonFloatingWindow();
  269.     }
  270.  
  271. FrontWindowUPP FrontWindowProcForAOCEUPP = NewFrontWindowProc(&FrontWindowProcForAOCE);
  272.  
  273. #endif